package com.yingxin.yxerp.module.message.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.id.IdHelper;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.message.entity.UserMessage;
import com.yingxin.yxerp.module.message.mapper.UserMessageMapper;
import com.yingxin.yxerp.module.message.service.IUserMessageService;
import com.yingxin.yxerp.module.message.vo.req.UserMessageReadBatchReqVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 用户消息 服务实现类
 * @author fanchao
 * @since 2022-03-21
 */

@Service
public class UserMessageServiceImpl
        extends ServiceImpl<UserMessageMapper, UserMessage>
        implements IUserMessageService {

    @Autowired
    private IUserSupport userSupport;

    @Override
    public Integer countUnreadByReceiverId(String userId) {
        QueryWrapper<UserMessage> qw = new QueryWrapper<>();
        qw.lambda().eq(UserMessage::getReceiverId, userId);
        qw.lambda().eq(UserMessage::getReadFlag, CommonConst.FALSE);
        return count(qw);
    }

    @Override
    public UserMessage getCount(String userId){
        QueryWrapper<UserMessage> qw = new QueryWrapper<>();
        qw.lambda().eq(UserMessage::getReceiverId, userId);
        qw.lambda().eq(UserMessage::getReadFlag, CommonConst.FALSE);
        qw.lambda().last("ORDER BY create_time DESC LIMIT 1");
        return getOne(qw);
    }

    /**
     * 创建
     *
     * @param message
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(UserMessage message) {
        message.setId(IdHelper.snowflakeId());
        save(message);
    }

    @Override
    public void create(UserMessage message, String senderId, String receiverId) {
        message.setId(IdHelper.snowflakeId());
        message.setSenderId(senderId);
        message.setReceiverId(receiverId);
        save(message);
    }

    /**
     * 标记已读
     */
    @Override
    public void read( String id ){
        if (StringUtils.isNotBlank(id)) {
            UpdateWrapper<UserMessage> uw = new UpdateWrapper<>();
            uw.lambda().set(UserMessage::getReadFlag, CommonConst.TRUE);
            uw.lambda().eq(UserMessage::getId, id);
            uw.lambda().eq(UserMessage::getReceiverId, userSupport.getUserId());
            update(uw);
        }
    }

    /**
     * 批量已读
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void readBatch(@RequestBody UserMessageReadBatchReqVo queryVo) {
        if (CollectionUtil.isNotEmpty(queryVo.getIds())) {
            UpdateWrapper<UserMessage> uw = new UpdateWrapper<>();
            uw.lambda().set(UserMessage::getReadFlag, CommonConst.TRUE);
            uw.lambda().in(UserMessage::getId, queryVo.getIds());
            uw.lambda().eq(UserMessage::getReceiverId, userSupport.getUserId());
            update(uw);
        }
    }

    /**
     * 全部已读
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void allRead(){
        UpdateWrapper<UserMessage> uw = new UpdateWrapper<>();
        uw.lambda().set(UserMessage::getReadFlag, CommonConst.TRUE);
        uw.lambda().eq(UserMessage::getReceiverId, userSupport.getUserId());
        update(uw);
    }

}
